package cs3343.project.family;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class StartGame implements ItemListener, ActionListener {
	
	JFrame f;
	public static Character[] character = new Character[6];
	
	//String[] select = {"None","Player","Computer"};	//JComboBox select Event	
	String[] select = new String[7];
	
	JComboBox[] comboList;
	JComboBox combo = null;
	JButton center;
	String warningMsg="";
	
	int comboActivateNum = 0; 
	public static int selectedPlayer;
	
	//public Start(JFrame frame, Container contentPane, JPanel panel){
	public StartGame(){
		//new EndGame();
		
		ProfileManager.readFromProfile();
		select[0] = "NONE";
		for (int i = 1; i <= 6; i++){
			select[i] = ProfileManager.profiles.get(String.valueOf(i)).getName();
			//System.out.println(i+ " "+ ProfileManager.profiles.get(String.valueOf(i-1)).getName());
		}
		
		f = new JFrame("Family");
		Container contentPane = f.getContentPane();
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,1));
		panel.setBorder(BorderFactory.createTitledBorder("Family - Character")); 
		JPanel title = new JPanel();
		JPanel playerPane = new JPanel();
		title.setLayout(new BorderLayout());
		playerPane.setLayout(new GridLayout(2,3));
		
		
		JButton left = new JButton();
		left.setText("Create Profile");
		
		Icon enter =  new ImageIcon(ConfigManager.IMAGE_PATH + "family.jpg");
		// TODO changed. if error, change back
		//JButton center = new JButton();
		center = new JButton();
		
		center.setRolloverEnabled(true);
		center.setIcon(enter);
		center.addActionListener(this);
		
		JButton right = new JButton();
		right.setText("Game Setting");
		
		title.add(left, BorderLayout.WEST);
		title.add(center, BorderLayout.CENTER);
		title.add(right, BorderLayout.EAST);
		title.setBorder(BorderFactory.createRaisedBevelBorder());
		
		left.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
				  new CreateProfile();
			  }
		});
		
		right.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
			  }
		});
		
		//Character Choose Box
		JPanel playerBox = null;
		JLabel picture = null;
		comboList = new JComboBox[7];

		for(int i = 0; i < 6; i++){
			playerBox = new JPanel();
			playerBox.setLayout(new BorderLayout());
			//TODO show pic of selected char
			//picture = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + ""+i+".jpg"));
			picture = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "ran.jpg"));
			combo = new JComboBox(select);
			combo.addItemListener(this);
			
			if(i != 0)
			combo.disable();
			
			comboList[i] = combo;
			playerBox.setBorder(BorderFactory.createTitledBorder("-Choose Character-"));
			playerBox.add(picture,BorderLayout.CENTER);
			if(i < 6)
			playerBox.add(combo,BorderLayout.SOUTH);
			playerPane.add(playerBox);
		}
		
		panel.add(title);
		panel.add(playerPane);
		contentPane.add(panel);
		f.setLocation(300,20);
		f.pack();
		f.show();
		f.setSize(800, 700);
		f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ System.exit(0); }});
	}

	public void actionPerformed(ActionEvent arg0) {
		
		//SatirTherapy.state = 1;
		//System.out.println(SatirTherapy.changeMode);
		for(int i = 0, j = 0; i < 6; i++){
			//System.out.println(String.valueOf(comboList[i].getSelectedIndex()));
			if (comboList[i].getSelectedIndex() != 0){
				character[i] = ProfileManager.profiles.get(String.valueOf(comboList[i].getSelectedIndex()));
				//character[j] = ProfileManager.profiles.get(String.valueOf(comboList[i].getSelectedIndex()));
				//j++;
			}
		}
		//System.out.println(comboList[i].getSelectedIndex());
		
		//TODO hardcode char creation
		//check whether there are at least 2 players
		
		selectedPlayer = 0;
		for(int i = 0; i < 6; i++){
			if(comboList[i].getSelectedIndex() != 0)
				selectedPlayer ++;
		}
		
			if(	selectedPlayer > 1 ){
				f.setVisible(false);
				new InitGame();
			}
			else{
				warningMsg="Please select at least 2 characters";
				JOptionPane.showMessageDialog(null,warningMsg,"Message",1);
			}
		
		//SatirTherapy.changeMode = true;

		
	}

	public void itemStateChanged(ItemEvent e) {
			if(comboList[comboActivateNum].getSelectedIndex() != 0 && comboActivateNum < 5)
				comboActivateNum++;
			
			if(comboActivateNum < 6)
			comboList[comboActivateNum].enable();
		
	}

	/*public void itemStateChanged(ItemEvent item) {
	    
		//create char
	    //Character character = new Character("SorJai", 'M', 0, new ImageIcon("src/profilePic.jpg"), new ImageIcon("src/char.jpg"), 100);
	    //JLabel img = new JLabel(character.getImage());
	    //JLabel icon = new JLabel(character.getIcon());
	    
		System.out.println("ITEMSTATECHANGED");
		
	    if (item.getSource() != comboList[0]) {
	    	
	    }
	    
		
	}*/
	public JComboBox getJComboBoxPlayer(int i){
		
		return this.comboList[i];
	}
	public JButton getCenter(){
		return this.center;
	}
	public String getWarningMsg(){
		return this.warningMsg;
	}
}